package com.smart.primary.sys.service.impl;

import com.smart.primary.common.util.MD5Util;
import com.smart.primary.common.util.StringUtil;
import com.smart.primary.common.util.Util;
import com.smart.primary.sys.dao.RoleMapper;
import com.smart.primary.sys.dao.UserMapper;
import com.smart.primary.sys.dao.UserRoleMapper;
import com.smart.primary.sys.service.OrgService;
import com.smart.primary.sys.service.UserService;
import com.smart.primary.sys.vo.Org;
import com.smart.primary.sys.vo.Role;
import com.smart.primary.sys.vo.User;
import com.smart.primary.sys.vo.UserRole;
import org.apache.commons.collections.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper sysUserMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMapper sysRoleMapper;
    @Resource
    private OrgService sysOrgService;

    public UserServiceImpl() {
    }

    public User queryObject(User sysUser) throws Exception {
        return (User)this.sysUserMapper.queryObject(sysUser);
    }

    public List<User> queryList(User sysUser) throws Exception {
        return this.sysUserMapper.queryList(sysUser);
    }

    public List<User> queryListByPage(User sysUser) throws Exception {
        return this.sysUserMapper.queryListByPage(sysUser);
    }

    public Integer queryCount(User sysUser) throws Exception {
        Integer ct = this.sysUserMapper.queryCount(sysUser);
        return ct == null ? 0 : ct;
    }

    public Integer insert(User sysUser) throws Exception {
        return this.sysUserMapper.insert(sysUser);
    }

    public Integer update(User sysUser) throws Exception {
        return this.sysUserMapper.update(sysUser);
    }

    public Integer delete(String userIds) throws Exception {
        if (userIds != null && !userIds.equals("")) {
            String[] userIdArr = userIds.split(",");
            String[] var3 = userIdArr;
            int var4 = userIdArr.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String userId = var3[var5];
                this.sysUserMapper.delete(userId);
            }
        }

        return 1;
    }

    public User login(User sysUser) throws Exception {
        User select = new User();
        select.setUserName(sysUser.getUserName());
        String pwd = MD5Util.getMD5Code(sysUser.getUserPwd());
        select.setUserPwd(pwd);
        select.setIsDelete(0);
        User user = (User)this.sysUserMapper.queryObject(select);
        return user;
    }

    public Integer save(User sysUser) throws Exception {
        Date createTime = new Date();
        sysUser.setCreateTime(createTime);
        sysUser.setUpdateTime(createTime);
        sysUser.setUserId(Util.getUUID());
        sysUser.setIsDelete(0);
        sysUser.setUserState(1);
        sysUser.setUserPwd(MD5Util.getMD5Code(sysUser.getUserPwd()));
        sysUser.setOrgNo("001001");
        this.insert(sysUser);
        UserRole suRole = new UserRole();
        suRole.setUserId(sysUser.getUserId());
        suRole.setRoleId("c0f7d4e9bc534ac9a3b9d516b1b58f92");
        suRole.setId(Util.getUUID());
        return this.userRoleMapper.insert(suRole);
    }

    public Integer saveOrUpdate(User sysUser) throws Exception {
        if (StringUtil.isBlank(sysUser.getUserId())) {
            Date createTime = new Date();
            sysUser.setCreateTime(createTime);
            sysUser.setUpdateTime(createTime);
            sysUser.setUserId(Util.getUUID());
            sysUser.setIsDelete(0);
            sysUser.setUserState(1);
            sysUser.setUserPwd(MD5Util.getMD5Code(sysUser.getUserPwd()));
            return this.insert(sysUser);
        } else {
            sysUser.setUpdateTime(new Date());
            return this.update(sysUser);
        }
    }

    public Integer setUserRole(UserRole sysUserRole) throws Exception {
        String userId = sysUserRole.getUserId();
        if (userId != null && !userId.equals("")) {
            String roleIds = sysUserRole.getRoleId();
            sysUserRole.setRoleId((String)null);
            this.userRoleMapper.delUserRole(sysUserRole);
            if (roleIds != null && !roleIds.equals("")) {
                String[] roleIdArr = roleIds.split(",");
                String[] var5 = roleIdArr;
                int var6 = roleIdArr.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    String roleId = var5[var7];
                    UserRole suRole = new UserRole();
                    suRole.setUserId(userId);
                    suRole.setRoleId(roleId);
                    suRole.setId(Util.getUUID());
                    this.userRoleMapper.insert(suRole);
                }
            }
        }

        return 1;
    }

    public Integer delUserRole(UserRole sysUserRole) throws Exception {
        return this.userRoleMapper.delUserRole(sysUserRole);
    }

    public List<Role> getRoleListByUserId(Role sysRole, String userId) throws Exception {
        sysRole.setRoleState(1);
        List<Role> roles = this.sysRoleMapper.queryListByPage(sysRole);
        if (StringUtil.isNotBlank(userId)) {
            UserRole sysUserRole = new UserRole();
            sysUserRole.setUserId(userId);
            List<UserRole> userRoles = this.userRoleMapper.getRoleListByUserId(sysUserRole);
            Iterator var6 = roles.iterator();

            while(var6.hasNext()) {
                Role role = (Role)var6.next();
                Iterator var8 = userRoles.iterator();

                while(var8.hasNext()) {
                    UserRole userRole = (UserRole)var8.next();
                    if (role.getRoleId().equals(userRole.getRoleId())) {
                        role.setChecked(true);
                    }
                }
            }
        }

        return roles;
    }

    public Integer resetPassWord(User sysUser) throws Exception {
        String userIds = sysUser.getUserId();
        if (userIds != null && !userIds.equals("")) {
            String[] userIdArr = userIds.split(",");
            String[] var4 = userIdArr;
            int var5 = userIdArr.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String userId = var4[var6];
                sysUser.setUserId(userId);
                User user = (User)this.sysUserMapper.queryObject(sysUser);
                user.setUserPwd(MD5Util.getMD5Code("123321"));
                this.sysUserMapper.update(user);
            }
        }

        return 1;
    }

    public List<Org> getOrgSelectData() throws Exception {
        new ArrayList();
        Org sysOrg = new Org();
        sysOrg.setOrgState(1);
        List<Org> sysOrgs = this.sysOrgService.queryList(sysOrg);
        return sysOrgs;
    }

    public String formVerify(User sysUser, String type) throws Exception {
        String msg = null;
        User queryObj = new User();
        if (StringUtil.isBlank(sysUser.getUserId())) {
            queryObj.setUserName(sysUser.getUserName());
            if (this.sysUserMapper.queryCount(queryObj) > 0) {
                msg = "用户名重复";
            }
        }

        return msg;
    }

    private void makeData(Org sysOrg, List<Map<String, Object>> dataList) throws Exception {
        Map<String, Object> map = new LinkedMap();
        map.put("name", sysOrg.getOrgName());
        map.put("value", sysOrg.getOrgNo());
        if (sysOrg.getIsLeaf() == 0) {
            List<Map<String, Object>> children = new ArrayList();
            Org query = new Org();
            query.setParentNo(sysOrg.getOrgNo());
            query.setOrgState(1);
            List<Org> loopList = this.sysOrgService.queryList(query);
            Iterator var7 = loopList.iterator();

            while(var7.hasNext()) {
                Org org = (Org)var7.next();
                this.makeData(org, children);
            }

            map.put("children", children);
        }

        dataList.add(map);
    }
}